<?php

// [ 优惠码 ]

namespace app\common\lib;
use think\Db;

class Code
{
    // 创建优惠码
    public static function create($orgId, $mobile)
    {
        $code = mkstr(16) . $orgId;
        $code = md5($code);
        $code = strtoupper($code);

        $data['code']        = $code;
        $data['org_id']      = $orgId;
        $data['mobile']      = $mobile;
        $data['project_id']  = 0;
        $data['status']      = 0;
        $data['create_time'] = time();
        $data['update_time'] = 0;
        $num = Db::name('code')->insert($data);
        return ($num > 0);
    }

    // 验证优惠码
    public static function check($code)
    {
        $ret = Db::name('code')->where('code',$code)->where('status',0)->find();
        return is_null($ret) ? false : true;
    }

    // 使用优惠码
    public static function used($code, $projectId)
    {
        $data['project_id']  = $projectId;
        $data['status']      = 1;
        $data['update_time'] = time();
        $num = Db::name('code')->where('code',$code)->update($data);
        return ($num > 0);
    }
}